const app = getApp()
Page({
  data: {
    uid:0,
    loading: true,
    showbar:false,
    show_follow:true,
    onLike:false,
    cate_id:0,
    active:1,
    icons:[],
    like:[],
    banner:[],
    info: [],
    page: 0
  },
  //载入
  onLoad: function (options) {
    app.getParam(options,(param) => {
      if (!app.util().isNull(param.ucode) && !app.util().isNull(param.id)) {
        wx.navigateTo({
          url: 'details?ucode=' + param.ucode + '&id=' + param.id
        })
      }
      if(!app.util().isNull(app.globalData.loginuser.uid)){
        this.setData({
          uid:app.globalData.loginuser.uid
        })
      }
      this.getApi();
    });
  },
  // 下拉刷新
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    setTimeout(function () {
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    }, 1500);
    this.setData({
      page: 0,
      loading: true,
      info: []
    });
    this.getInfo();
  },
  //上拉加载
  onReachBottom: function () {
    this.setData({
      loading:true
    });
    this.getInfo();
  },
  //获取城市云信息统计
  getApi: function () {
    app.api().Get('api/v4/allwin/info/cate',{signkey:app.util().getRandom(32)},(result)=> {
      var icons = [{id:0, name:"关注"},{id:0, name:"热门"}];
      this.setData({
        icons:icons.concat(result.data)
      });
      this.getInfo();
    })
    app.api().Get('api/v4/allwin/adwords/index',{ids:'info'},(result)=>{
      if (result.code == 200){
        this.setData({
          banner: result.data.info,
        })
      }
    })
  },
  //获取首页接口
  getInfo: function () {
    if (this.data.loading) {
      var param = {
        cate_id:this.data.cate_id,
        page: this.data.page + 1,
        state:this.data.active,
      }
      app.api().Get('api/v4/allwin/info/index',param,(result)=>{
        if (result.code == 200) {
          var info = this.data.info;
          for (let i in result.data) {
            info.push(result.data[i]);
          }
          this.setData({
            info: info,
          });
          this.data.page = param.page
        }
        this.setData({
          loading: false,
        });
      })
    }
  },
  //点赞
  onLike:function(e){
    let key = e.currentTarget.dataset.key,id = e.currentTarget.dataset.id;
    var info = this.data.info;
    if (info[key].is_like == 0){
      app.api().Post("api/v4/allwin/infosend/like",{id:id},(rel) => {
        info[key].is_like = 1;
        info[key].like    = info[key].like+1;
        this.setData({
          info:info,
          onLike:true
        })
        setTimeout(()=>{
          this.setData({
            onLike:false
          })
        },600)
      })
    }
  },
  //关注与取消
  onFollow:function (e) {
    var key = e.currentTarget.dataset.key;
    var info = this.data.info;
    if (info[key].is_follow == 0){
      app.api().Post("api/v4/allwin/infosend/follow",{uid:info[key].user_id,state:1},(rel) => {
        info[key].is_follow = 1;
        this.setData({
          info:info,
        })
      })
    }
  },
  //点击加载更多
  onChange: function (event) {
    var active = event.detail.index, icons = this.data.icons;
    this.setData({
      page: 0,
      loading: true,
      active: active,
      info: [],
    });
    this.data.cate_id = icons[active].id;
    this.getInfo();
  },
  //拨打电话
  callPhone:function (e) {
    app.callPhone(e);
  },
  //图片预览
  previewImage: function (e) {
    var key = e.target.dataset.key;
    wx.previewImage({
      current: this.data.info[key].img,
      urls: this.data.info[key].images
    })
  },
  //去发信息
  send:function () {
    wx.navigateTo({
      url: 'send',
    })
  },
  //搜索
  onSearch: function (e) {
    wx.navigateTo({
      url: 'search?keyword='+e.detail,
    })
  },
  //分享按钮
  onShareAppMessage: function (e) {
    return {
      title: app.globalData.appConfig.shore_text,
      imageUrl:app.globalData.appConfig.shore_img,
      path: '/pages/info/index?ucode=' + app.globalData.loginuser.ucode
    }
  },
  //滚动
  onPageScroll(res){
    let scrollTop = res.scrollTop;
    this.setData({
      showbar: 54 <= scrollTop ?true :false
    })
  }
})